import permissionApi from "@/api/acl/permission";
import { Message } from "element-ui";

const state = {
  permissionList:[],
  expandKeys:[],

}

const mutations = {
  SET_PERMISSIONLIST(state,data){
    state.permissionList = data
    state.expandKeys.push(data[0].id)
  }
}

const actions = {
  async getPermissionList({commit}){
    try {
      const res = await permissionApi.getPermissionList()
      if(res && res.code === 20000){
        console.log(res.data);
        commit('SET_PERMISSIONLIST',res.data.children)
      }
    } catch (error) {
      console.error(error);
    }
  },
  async removePermission({commit},id){
    try {
      const res = await permissionApi.removePermission(id)
      if(res && res.code === 20000){
        Message.success('删除成功')
      }else {
        Message.error('删除失败')
      }
    } catch (error) {
      console.error(error);
      Message.error('删除失败')
    }
  },
  async addPermission({commit},permission){
    try {
      const res = await permissionApi.addPermission(permission)
      if(res && res.code === 20000){
        Message.success('添加成功')
      }else {
        Message.error('添加失败')
      }
    } catch (error) {
      console.error(error);
      Message.error('添加失败')
    }
  },
  async updatePermission({commit},permission){
    try {
      const res = await permissionApi.updatePermission(permission)
      if(res && res.code === 20000){
        Message.success('修改成功')
      }else {
        Message.error('修改失败')
      }
    } catch (error) {
      console.error(error);
      Message.error('修改失败')
    }
  }
}

export default {
  state,
  mutations,
  actions,
  namespaced:true
}